﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Dynamic;

using Tango.Dynamic;
using Tango.Scripting;

namespace Tango.ExpressionCompiler.DynamicExpression.Binders
{
	class UnaryBinder : BinderBase
	{
		private readonly Func<Expression,Expression> m_Factory;

		public UnaryBinder(Func<Expression,Expression> factory)
		{
			m_Factory=factory;
		}

		public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
		{
			var restrictions=BindingRestrictions.Empty;
			Expression expression=null;

			var instance=target.GetLimitedExpression();
			expression=m_Factory(instance);
			
			expression=expression.ConvertTo(this.ReturnType);
			restrictions=restrictions.AndLimitType(target);

			return new DynamicMetaObject(expression,restrictions);
		}
	}
}
